package com.lws.omapicompat.simalliance

import android.content.Context
import com.lws.omapicompat.ReaderCompat
import com.lws.omapicompat.SEServiceCompat
import org.simalliance.openmobileapi.Reader
import org.simalliance.openmobileapi.SEService
import java.util.concurrent.Executor

class SEServiceOld(
    context: Context,
    executor: Executor,
    listener: SEServiceCompat.OnConnectedListener
) : SEServiceCompat {


    private val seService: SEService = SEService(context) {
        listener.onConnected()
    }


    override fun getReaders(): Array<ReaderCompat> {
        return seService.readers.filterNotNull().map {
            ReaderOld(this, it)
        }.toTypedArray()
    }

    override fun getUiccReader(slotNumber: Int): ReaderCompat {
        val reader = seService.readers.find { reader: Reader ->
            return@find reader.name == "SIM$slotNumber" || reader.name == "UICC$slotNumber"
        }
            ?: throw IllegalArgumentException("the reader object corresponding to the uiccSlotNumber is not exist.")

        return ReaderOld(this, reader)
    }

    override fun getVersion(): String {
        return seService.version
    }

    override fun isConnected(): Boolean {
        return seService.isConnected
    }


    override fun shutdown() {
        seService.shutdown()
    }
}